package com.bw.controller;

import com.bw.bean.Geshou;
import com.bw.bean.Middle;
import com.bw.bean.Song;
import com.bw.service.SongService;
import com.bw.utils.FileUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SongController {

    @Autowired
    private SongService ss;

    //全查
    @RequestMapping("list.do")
    public String findAllSong(Model model
            ,@RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "") String start
            ,@RequestParam(defaultValue = "") String end){
        PageHelper.startPage(pageNum,3);

        Map<String,Object> map = new HashMap<>();
        map.put("start",start);
        map.put("end",end);
        List<Song> list = ss.findAllSong(map);

        PageInfo plist = new PageInfo(list);
        model.addAttribute("plist",plist);
        model.addAttribute("start",start);
        model.addAttribute("end",end);
        return "list";
    }

    //歌手信息
    @RequestMapping("geshou.do")
    @ResponseBody
    public List<Geshou> geShous(){
        List<Geshou> list = ss.geShous();
        return list;
    }


    //添加信息
    @RequestMapping("add.do")
    public String adds(Song song, @RequestParam("myFile") CommonsMultipartFile myFile, Integer[] ge, HttpServletRequest request) throws IOException {

        String realPath = request.getServletContext().getRealPath("upload");

        String filename = myFile.getOriginalFilename();

        File file = new File(realPath + "/" + filename);

        myFile.transferTo(file);

        song.setSpath("upload/"+filename);
        song.setScore(0d);
        int i = ss.addSong(song);
        if(i>0){
            for (Integer gid : ge) {
                Middle middle = new Middle(null,song.getSid(),gid);
                ss.addMiddle(middle);
            }
            return "forward:list.do";
        }
        return "index";
    }

    //删除信息
    @RequestMapping("dels.do")
    public String dels(Integer sid){
        int i = ss.delSong(sid);
        if(i>0){
            ss.delMiddle(sid);
            return "forward:list.do";
        }
        return "index";
    }


    //全查
    @RequestMapping("select.do")
    @ResponseBody
    public Song select(Integer sid){
        Song song = ss.select(sid);
        return song;
    }


    //评分
    @RequestMapping("ping.do")
    public String select(Integer sid,Integer score){
        ss.pingfen(sid,score);
        return "forward:list.do";
    }


    @RequestMapping("pai.do")
    public String pai(Model model){
        List<Song> list = ss.pai();
        model.addAttribute("list",list);
        return "pai";
    }


    //下载
    @RequestMapping("down.do")
    public void down(@RequestParam("filep") String filep, HttpServletRequest request, HttpServletResponse response) throws Exception {

        FileUtils.download(filep,request,response);
    }

    //所有的歌手
    @RequestMapping("huixian.do")
    @ResponseBody
    public List<Geshou> geshouss(){
        List<Geshou> list = ss.gesous();
        return list;
    }
}
